package com.work.servlet.student;

import com.work.bean.Work;
import com.work.service.MiddleService;
import com.work.service.WorkService;
import com.work.service.impl.MiddleServiceImpl;
import com.work.service.impl.WorkServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/10/8.
 */
@WebServlet("/stu_history_work")
public class StuHistoryWorkServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        HttpSession session = req.getSession();

        if (session.getAttribute("isLogin") != null && (Boolean) session.getAttribute("isLogin")) {

            MiddleService service = new MiddleServiceImpl();

            int countPage, whichPage;

            HashMap<Integer, List> mapMiddle;

            //分页
            if (req.getParameter("countPage") == null && req.getParameter("whichPage") == null) {
                countPage = 10;
                whichPage = 1;
            } else {
                countPage = Integer.parseInt(req.getParameter("countPage"));
                whichPage = Integer.parseInt(req.getParameter("whichPage"));
            }
            int isFinish ;

            if (req.getParameter("isFinish") != null){
                isFinish = Integer.parseInt(req.getParameter("isFinish"));
                session.setAttribute("isFinish", isFinish);
            }else {
                isFinish = Integer.parseInt(String.valueOf(session.getAttribute("isFinish")));
            }

            mapMiddle = service.pageByStudentId(countPage, whichPage,isFinish, (Integer) session.getAttribute("studentId"));

            for (Map.Entry<Integer, List> m : mapMiddle.entrySet()) {

                session.setAttribute("myWorks", m.getValue());
                session.setAttribute("pageNumber", m.getKey());
            }

            session.setAttribute("countPage", countPage);
            session.setAttribute("whichPage", whichPage);

            req.getRequestDispatcher("student_history_work.jsp").forward(req, resp);

        } else {
            session.setAttribute("error", "请登录后访问");
            resp.sendRedirect("login.jsp");
        }

    }

}

